<template>
  <div class="app-container">
    <div class="main">
      <div class="filter-container g-bgc-fill-el-input">
        <div class="filter-left">
          <el-date-picker v-model="listQuery.date" type="daterange" @change="comHandleFilterDateChange"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" style="width: 208px" range-separator=""
            start-placeholder="开始时间" end-placeholder="结束时间">
          </el-date-picker>
          <el-select v-model="listQuery.type" placeholder="消息类型" clearable @change="comHandleFilterSelectChange"
            style="width: 136px" class="filter-item">
            <el-option v-for="(item, index) in typeOptions" :key="'br' + index" :label="item.label" :value="item.id" />
          </el-select>
          <!-- <el-select v-model="listQuery.menufacturerId" placeholder="品牌" clearable @change="comHandleFilterSelectChange"
            style="width: 90px" class="filter-item">
            <el-option v-for="(item, index) in brandOptions" :key="'br' + index" :label="item.menufacturer"
              :value="item.id" />
          </el-select> -->
          <!-- 创建人 后期要调 -->
          <!-- <el-select v-model="listQuery.createBy" placeholder="创建人" clearable @change="comHandleFilterSelectChange"
            style="width: 122px" class="filter-item">
            <el-option v-for="(item, index) in brandOptions" :key="'br' + index" :label="item.menufacturer"
            :value="item.id" />
          </el-select> -->
        </div>
        <div class="filter-right">
          <el-button type="warning" @click="handleNewAddPush">新增推送</el-button>
        </div>
      </div>

      <el-table class="g-el-table" :data="list" v-loading="listLoading">
        <el-table-column label="消息推送" prop="deviceCode" min-width="180px">
          <div slot-scope="{row}">
            {{ row.createDate }}
            （{{ row.type | typeFilter }}）
          </div>
        </el-table-column>
        <el-table-column label="品牌">
          <template slot-scope="{row}">
            <span>{{ row.menufacturerId }}</span>
          </template>
        </el-table-column>
        <el-table-column label="推送方式">
          <template slot-scope="{row}">
            <span>{{ row.pushBy | pushTypeFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人">
          <template slot-scope="{row}">
            <span>{{ row.createBy }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleView(row)">查看</el-button>
            <!-- <el-button type="text" @click="handleAddPush(row)">增加推送</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <np-pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>

<script>
import { getMenufacturers } from "@/api/family-and-users-management/index"
import { fetchList, delMenufacturer } from '@/api/message-management/family/list'

const typeOptions = [
  { id: 1, label: '系统消息' },
  { id: 3, label: '升级消息' },
  { id: 4, label: '日志消息' },
  { id: 5, label: '隐私协议变更消息' },
  { id: 6, label: '隐私协议变更消息' },
];

export default {
  name: 'ComplexTable',
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,

        founder: undefined, // 创建人 没有这个字段
        phoneNum: undefined, // 品牌名称

        startDate: undefined,
        endDate: undefined,
      },
      brandOptions: [],
      typeOptions,
    }
  },
  filters: {
    typeFilter(type) {
      let v = typeOptions.find(el => type === el.id)
      if (v) {
        return v.label
      }
      return ''
    },
    pushTypeFilter(type) {
      // 推送方式数组
      const pushTypeOptions = [
        { id: 1, label: '全部用户' },
        { id: 2, label: '指定用户' },
      ];
      let v = pushTypeOptions.find(el => type === el.id)
      if (v) {
        return v.label
      }
      return ''
    },
  },
  computed: {
  },
  created() {
    this.getList()
    // 获取品牌
    getMenufacturers({ pageNum: 1, pageSize: 1000 }).then(res => {
      if (res.code === 200) {
        this.brandOptions = res.data.list || []
      }
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      let pans = { ...this.listQuery }
      delete pans.date
      fetchList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.list = res.data.list;
          this.total = res.data.total
        }

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    comHandleFilterSelectChange(e) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterDateChange(e) {
      this.listQuery.pageNum = 1
      this.listQuery.startDate = e ? e[0] : ''
      this.listQuery.endDate = e ? e[1] : ''
      this.getList()
    },
    handleNewAddPush() {
      this.$router.push('/message/family/add')
    },
    handleView(row) {
      this.$router.push({
        path: '/message/family/details',
        query: {
          id: row.id || ''
        }
      })
    },
    handleAddPush(row) {
      this.editAddInfoRow = row
      this.dialogEditAddNewBrandVisible = true
    },
  }
}
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  overflow: auto;
}

.main {
  background-color: #fff;
  border-radius: 16px;
  padding: 20px 20px 28px;
}
</style>